/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package justdoit.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import justdoit.controller.TaskManager;
import justdoit.model.DefaultSettings;
import justdoit.model.User;
import justdoit.controller.LanguageManager;
import justdoit.controller.ThemeManager;
import justdoit.controller.XMLManager;
import justdoit.actions.AddNewTaskAction;

/**
 * This is the main window 
 * which is supposed to contain the task list and everything else needed.
 * @author Can
 */
public class MainFrame extends JFrame {

    private DefaultSettings defaultSettings;
    private User user;
    // Components
    /**
     * MainFrame contains menuBar + clientPanel;
     * clientPanel contains actionPanel + taskListPanel + statusBar
     * actionPanel contains comboBox, buttons, and so on
     * taskListPanel is responsible to display tasks
     * statusBar display additional information like current user, time...
     */
    private TaskMenuBar menuBar;
    private JPanel clientPanel;
    private JPanel actionPanel;
    private TaskTablePanel taskListPanel;
    private StatusBar statusBar;
    // action
    private JButton addNewTaskButton;
    private JComboBox viewComboBox;
    private String[] viewOptions = {
        LanguageManager.getString("combo.view.pendingTasks"),
        LanguageManager.getString("combo.view.completedTasks"),
        LanguageManager.getString("combo.view.overdueTasks")
    };

    public MainFrame(DefaultSettings ds, User user) {
        if (ds != null) {
            this.defaultSettings = ds;
        } else {
            this.defaultSettings = new DefaultSettings();
        }
        this.user = user;
        this.initComponents();
        // view
        if (this.defaultSettings.getMode().equals(DefaultSettings.MODE_COMPACT)) {
            compactView();
        } else {
            normalView();
        }
        // theme
        if (user.getTheme() != null && user.getTheme() != "") {
            ThemeManager.setTheme(user.getTheme());
        }
    }

    private void initComponents() {
        menuBar = new TaskMenuBar();
        clientPanel = new JPanel();
        actionPanel = new JPanel();
        taskListPanel = new TaskTablePanel(this.user, TaskTablePanel.MODE_TABLE, TaskTablePanel.VIEW_PENDING);
        viewComboBox = new JComboBox(viewOptions);
        viewComboBox.setToolTipText(LanguageManager.getString("tooltip.viewTasks"));

        addNewTaskButton = new JButton(new AddNewTaskAction());
        addNewTaskButton.setToolTipText(LanguageManager.getString("tooltip.addNewTask"));

        this.setLayout(new BorderLayout());
        this.add(BorderLayout.NORTH, menuBar);
        this.add(BorderLayout.CENTER, clientPanel);

        statusBar = new StatusBar(user);

        clientPanel.setLayout(new BorderLayout());
        clientPanel.add(BorderLayout.NORTH, actionPanel);
        clientPanel.add(BorderLayout.CENTER, taskListPanel);
        clientPanel.add(BorderLayout.SOUTH, statusBar);

        actionPanel.setLayout(new BorderLayout());
        actionPanel.add(BorderLayout.EAST, addNewTaskButton);
        actionPanel.add(BorderLayout.WEST, viewComboBox);

        viewComboBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JComboBox cb = (JComboBox) e.getSource();
                int selectedIndex = cb.getSelectedIndex();
                String viewContent;
                switch (selectedIndex) {
                    case 0:
                        viewContent = TaskTablePanel.VIEW_PENDING;
                        break;
                    case 1:
                        viewContent = TaskTablePanel.VIEW_COMPLETED;
                        break;
                    case 2:
                        viewContent = TaskTablePanel.VIEW_OVERDUE;
                        break;
                    default:
                        System.out.println("Unexpected Error! : viewComboBox");
                        viewContent = TaskTablePanel.VIEW_PENDING;
                        break;
                }
                taskListPanel.changeView(viewContent);
            }
        });

        this.setTitle("JustDoIt " + LanguageManager.getString("title.mainFrame"));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setMinimumSize(new Dimension(400, 300));
        this.setSize(this.defaultSettings.getNormalWidth(), this.defaultSettings.getNormalHeight());
        if (this.defaultSettings.getWinX() == 0
                && this.defaultSettings.getWinY() == 0) {
            this.setLocationRelativeTo(null);
        } else {
            this.setLocation(this.defaultSettings.getWinX(), this.defaultSettings.getWinY());
        }

        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                saveDefaultSettings();
            }
        });
    }

    // save windows position, size and so on, in order to restore the similar
    // session next time start up
    public void saveDefaultSettings() {
        if (this.defaultSettings.getMode().equals(DefaultSettings.MODE_NORMAL)) {
            this.defaultSettings.setWinX(this.getX());
            this.defaultSettings.setWinY(this.getY());
            this.defaultSettings.setNormalWidth(this.getWidth());
            this.defaultSettings.setNormalHeight(this.getHeight());
        } else if (this.defaultSettings.getMode().equals(DefaultSettings.MODE_COMPACT)) {
            this.defaultSettings.setCompactDimension(this.getSize());
            this.defaultSettings.setCompactLocation(this.getLocation());
        }
        if (this.defaultSettings.getUserName() != "") {
            this.defaultSettings.setUserName(TaskManager.getCurrentUser().getUserName());
        }
        this.defaultSettings.setLocale(TaskManager.getCurrentUser().getLocale());
        XMLManager xm = new XMLManager();
        xm.updateDefaultSettings(this.defaultSettings);
    }

    public TaskTablePanel getTaskListPanel() {
        return taskListPanel;
    }

    public void updateColor(Color color) {
        clientPanel.setBackground(color);
        actionPanel.setBackground(color);
        menuBar.setBackground(color);
        taskListPanel.setTaskTableListBackground(color);
        statusBar.setBackground(color);
    }

    // change to compact view
    public final void compactView() {
        this.taskListPanel.setMode(TaskTablePanel.MODE_LIST);
        this.taskListPanel.setVisible(true);
        // save the normal view parameters
        if (this.defaultSettings.getMode().equals(DefaultSettings.MODE_NORMAL)) {
            this.defaultSettings.setWinX(this.getX());
            this.defaultSettings.setWinY(this.getY());
            this.defaultSettings.setNormalWidth(this.getWidth());
            this.defaultSettings.setNormalHeight(this.getHeight());
            this.taskListPanel.refreshData();
        }
        // set the compact view parameters
        this.setSize(this.defaultSettings.getCompactDimension());
        if (this.defaultSettings.getCompactLocation().x != 0
                || this.defaultSettings.getCompactLocation().y != 0) {
            this.setLocation(this.defaultSettings.getCompactLocation());
        } else {
            this.setLocationRelativeTo(null);
        }
        this.defaultSettings.setMode(DefaultSettings.MODE_COMPACT);
    }

    // change to normal view
    public final void normalView() {
        this.taskListPanel.setMode(TaskTablePanel.MODE_TABLE);
        this.taskListPanel.setVisible(true);
        if (this.defaultSettings.getMode().equals(DefaultSettings.MODE_COMPACT)) {
            this.defaultSettings.setCompactDimension(this.getSize());
            this.defaultSettings.setCompactLocation(this.getLocation());
            this.taskListPanel.refreshData();
        }
        this.setSize(new Dimension(this.defaultSettings.getNormalWidth(), this.defaultSettings.getNormalHeight()));
        if (this.defaultSettings.getWinX() == 0
                && this.defaultSettings.getWinY() == 0) {
            this.setLocationRelativeTo(null);
        } else {
            this.setLocation(this.defaultSettings.getWinX(), this.defaultSettings.getWinY());
        }
        this.defaultSettings.setMode(DefaultSettings.MODE_NORMAL);
    }

    public final void showStatusBar(boolean bShow) {
        statusBar.setVisible(bShow);
    }

    public void updateUserNameLabel() {
        this.statusBar.updateUserNameLabel();
    }
}
